package com.example.demo01.service;

import com.example.demo01.mapper.FormInfoMapper;
import com.example.demo01.mapper.RecordMapper;
import com.example.demo01.mapper.UserMapper;
import com.example.demo01.pojo.FormInfo;
import com.example.demo01.pojo.User;
import com.example.demo01.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RecordMapper recordMapper;
    @Autowired
    FormInfoMapper formInfoMapper;

    public List<User> getAllUsers(){
        return userMapper.getAllUsers();
    }

    public List<FormInfo> getAllFormInfos(){
        return formInfoMapper.getAllFormInfos();
    }

    //有可能账号被冻结了
    public List<UserInfo> getAllUserInfo(){
        List<User> users=getAllUsers();

        List<FormInfo> formInfos=getAllFormInfos();


        Map<String, String> map=new HashMap<>();
        for(int i=0;i<users.size();++i){
            map.put(users.get(i).getUsername(), users.get(i).getActive());
        }

        List<UserInfo> userInfos=new ArrayList<>();
        for(FormInfo f : formInfos){
            userInfos.add(new UserInfo(f.getUsername(),f.getName(),f.getPhone(),f.getLocation(),f.getHealth(),f.getIsPostToday()
            ,map.get(f.getUsername())));
        }
        //<username, active>由于active字段是在user表，而其余信息是在form_info表，所以要合并两张表，通过username连接，其实也可以在数据库关联查找
        //一种是代码实现，一种是sql语句实现

        return userInfos;
    }

    public List<User> getAllAdmins(){
        List<User> admins=userMapper.getAllAdmins();
        return admins;
    }


}
